import { SessionProvider, useSession } from "next-auth/react";
import type { AppProps } from "next/app";
import "../styles/App.css";
import { ConfigProvider } from "antd";
import locale from "antd/lib/locale-provider/zh_CN";
import "moment/locale/zh-cn";
import Router from "next/router";
import LoadingSpin from "../components/spin-loading";

// TODO: fix that type warning
export default function App({
  Component,
  pageProps: { session, ...pageProps },
}: AppProps) {
  console.log("component.auth:", Component.auth);
  return (
    <SessionProvider session={session}>
      <ConfigProvider locale={locale}>
        {Component.auth == true ? (
          <Auth>
            <Component {...pageProps} />
          </Auth>
        ) : (
          <Component {...pageProps} />
        )}
      </ConfigProvider>
    </SessionProvider>
  );
}
function Auth({ children }) {
  const { status } = useSession();

  if (status === "loading") {
    return <LoadingSpin />;
  } else if (status === "unauthenticated") {
    Router.push("/signin");
  } else if (status === "authenticated") {
    return children;
  }
}
